﻿using Verse;

namespace ARMIR {
    public class HediffComp_MindShield : HediffComp {
        public HediffCompProperties_MindShield Props => (HediffCompProperties_MindShield)props;

        private int ticksToNextEffecter;
        public override void CompPostMake()
        {
            base.CompPostMake();
            ticksToNextEffecter = Props.ticksToNextEffecter;
        }
        public override void CompPostTick(ref float severityAdjustment)
        {
            base.CompPostTick(ref severityAdjustment);
            // 只在pawn存活且在地图上时生效
            if (Pawn.Dead || Pawn.Map == null) return;
            // 倒计时触发效果
            ticksToNextEffecter--;
            if (ticksToNextEffecter <= 0)
            {
                tryTriggerEffecter();
                tryRemoveHediff();
                ticksToNextEffecter = Props.ticksToNextEffecter;
            }
        }
        //周期性触发效果
        private void tryTriggerEffecter() {
            EffecterDef shieldEffecter = Props.effecter;
            shieldEffecter.SpawnAttached(Pawn, Pawn.Map);
        }
        //如果所有者拥有该hediff但不拥有护盾则清理hediff
        private void tryRemoveHediff() {
            if (!(Pawn.apparel.WornApparel.Any(a => a.def == Props.requiresEquipment) ||
                (Pawn.equipment.AllEquipmentListForReading.Any(e => e.def == Props.requiresEquipment))))
            {
                Pawn.health.RemoveHediff(parent);
            }
        }
    }
}
